﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Compilation;
using DotNetNuke;
using DotNetNuke.Data;
using System.Data.SqlClient;
using System.Data;

namespace ReflectionTesting
{
    class Program
    {
        static void Main(string[] args)
        {

            /********************************************************************************  
             * Because the default alogrithm of DNN Reflection.vb whould constructure 
             * the string like "class name + assebmly name" if you just provide the TypeName
             * as the input parameter, so we manually provider the full information string
             * to CacheTypes function, create the type and cached it. 
             * 
             * 
             ******************************************************************************/
            CacheTypes("DotNetNuke.Security.Membership.Data.SqlDataProvider,DotNetNuke.Membership.Dataprovider:DotNetNuke.Security.Membership.Data.SqlDataProvider;DotNetNuke.Data.SqlDataProvider,DotNetNuke.SqlDataProvider:DotNetNuke.Data.SqlDataProvider");


            SqlDataReader objDataReader =  (SqlDataReader)DataProvider.Instance().GetPortal(0);
            DataTable objTable = DotNetNuke.Common.Globals.ConvertDataReaderToDataTable(objDataReader);
            foreach (DataColumn column in objTable.Columns)
            {

                    Console.Write(column.ColumnName + " ");

            }
            
            DotNetNuke.Framework.Reflection.CreateObject("members");

            //Type objType = BuildManager.GetType("DotNetNuke.Security.Membership.AspNetMembershipProvider, DotNetNuke.Provider.AspNetProvider", true, true);
            //Type objType = BuildManager.GetType("DotNetNuke.Security.Membership.AspNetMembershipProvider, DotNetNuke.Provider.AspNetProvider", true, true);

        }

        private static void CacheTypes(string cacheTypeList)
        {
            if (cacheTypeList != null)
            {
                string[] cacheTypes = cacheTypeList.Split(';');
                foreach (string cacheType in cacheTypes)
                {
                    if (cacheType != null && cacheType.Length > 0)
                    {
                        string[] parts = cacheType.Split(':');
                        string type = parts[0];
                        string cacheKey = parts[1];
                        object typeObject = DotNetNuke.Framework.Reflection.CreateType(type, cacheKey, true, true);
                        if (typeObject == null)
                            Console.WriteLine("Type failed on pre-load: " + type);
                        else
                            Console.WriteLine("Type load succeeded : " + type);
                    }
                }
            }
        }
    }
}
